/*
 * Copyright (c) 2018. czmc All Rights Reserved.
 * Author: czmc
 * Email: zxc6355533@163.com
 * Encoding: UTF-8
 */

package top.stime21.myneighbours.module.home

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import kotlinx.android.synthetic.main.fragment_all.*
import top.stime21.myneighbours.R
import top.stime21.myneighbours.base.BuzMvpFragment
import top.stime21.myneighbours.data.entity.Need
import top.stime21.myneighbours.module.login.NeedContract
import top.stime21.myneighbours.module.login.presenter.NeedPrensenterImpl

/**
 * Author: czmc  Email: zxc6355533@163.com
 * Version:	1.0  Date:2018/3/1
 */

class AllFragment : BuzMvpFragment<NeedContract.Presenter>(),NeedContract.View{
    override fun onDetailsGetSuccess(data: Need?) {
    }

    override fun onFinishSuccess() {
    }

    override fun onMyNeedGetSuccess(data: List<Need>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onMyNeedMoreGetSuccess(data: List<Need>?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onPublishSuccess() {
    }

    override fun onAcceptSuccess() {
    }

    override fun onCancleSuccess() {
    }

    override fun onNearbySuccess(data: List<Need>?) {
    }

    override fun onSearchSuccess(data: List<Need>?) {
        ( recycleView.adapter as NeedAdapter).setUpdateData(data)
    }

    override fun getPresenter(): NeedContract.Presenter {
        return NeedPrensenterImpl(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_all
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadingLayout.setRetryListener {
            var key = et_content.text.toString()
            if(TextUtils.isEmpty(key)){
                key=""
            }
            mPrensenter!!.search(key)
        }
        recycleView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        recycleView.adapter = NeedAdapter(context!!)
        et_content.addTextChangedListener(watch)
        mPrensenter!!.search("")
        refreshView.setOnRefreshListener {   mPrensenter!!.search("") }
    }

    private val watch:TextWatcher=object:TextWatcher{
        override fun afterTextChanged(p0: Editable?) {
        }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            var key = et_content.text.toString()
            if(TextUtils.isEmpty(key)){
                key=""
            }
            mPrensenter!!.search(key)
        }

    }

    override fun showViewLoadding() {
        loadingLayout!!.showLoading()
    }

    override fun showEmpty() {
        loadingLayout!!.showEmpty()
        refreshView.isRefreshing = false
    }

    override fun showContent() {
        loadingLayout!!.showContent()
        refreshView.isRefreshing = false
    }

    override fun showError() {
        loadingLayout!!.showError()
        refreshView.isRefreshing = false
    }

}